package com.andy.service.impl;

import com.andy.infrastructure.util.FileUtil;
import com.andy.infrastructure.util.FreeMarkerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Map;

@Service
public class AbiCreateTableService {

    @Autowired
    private freemarker.template.Configuration cfg;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private FreeMarkerUtil freeMarkerUtil;

    private static final String TOTLE_FILE_PATH = "D:\\autoOutput";


    public void createTableSql(String input){
        //创建controller
        doCreateEnumClass(input);

        //弹出框体
        FileUtil.popupDir(TOTLE_FILE_PATH);
    }

    /**
     * 创建controller
     * @param input
     */
    private void doCreateEnumClass(String input){
        //1-class的内容
        String ftlName = "abi_table.ftl";

        Map<String, Object> map =  freeMarkerUtil.putInput(input);

        String ftlString = freeMarkerUtil.getFreeMarkerString(cfg,ftlName,input);
        //生成文件
        String fileName = (String)map.get("tableName")+".txt";
        String filePath = TOTLE_FILE_PATH+"\\table";
        try {
            //创建文件
            FileUtil.createFile(fileName,ftlString,filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
